<html>
<head><meta charset="utf-8"><title>Doctest weirdness · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Doctest.20weirdness.html">Doctest weirdness</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="214120653"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Doctest%20weirdness/near/214120653" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Doctest.20weirdness.html#214120653">(Oct 21 2020 at 22:03)</a>:</h4>
<p>I'm getting a weird error from doctest, "unresolved import <code>super::foo</code>", when  in a module defined in the test. Better example:<br>
<a href="http://lib.rs">lib.rs</a></p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="sd">///</span>
<span class="sd">/// ```rust</span>
<span class="sd">/// fn foo() {}</span>
<span class="sd">///</span>
<span class="sd">/// mod bar {</span>
<span class="sd">///     use super::foo;</span>
<span class="sd">/// }</span>
<span class="sd">/// ```</span>
<span class="sd">///</span>
<span class="k">fn</span> <span class="nf">whatever</span><span class="p">()</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
</code></pre></div>

<p>The code in the doctest works <em>outside</em> of the doctest. I'm on version <code>rustc 1.48.0-nightly (a1947b3f9 2020-09-10)</code></p>



<a name="214120932"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Doctest%20weirdness/near/214120932" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Doctest.20weirdness.html#214120932">(Oct 21 2020 at 22:06)</a>:</h4>
<p>Just throwing ideas: maybe doctests get compiled as a separate crate?</p>



<a name="214121000"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Doctest%20weirdness/near/214121000" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Poliorcetics <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Doctest.20weirdness.html#214121000">(Oct 21 2020 at 22:06)</a>:</h4>
<p>They do</p>



<a name="214121020"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Doctest%20weirdness/near/214121020" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Poliorcetics <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Doctest.20weirdness.html#214121020">(Oct 21 2020 at 22:06)</a>:</h4>
<p>You have to import everything</p>



<a name="214121049"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Doctest%20weirdness/near/214121049" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Poliorcetics <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Doctest.20weirdness.html#214121049">(Oct 21 2020 at 22:07)</a>:</h4>
<p>Docs are for user so they should not depend on private implementation details</p>



<a name="214121153"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Doctest%20weirdness/near/214121153" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Doctest.20weirdness.html#214121153">(Oct 21 2020 at 22:08)</a>:</h4>
<p>That makes sense, but that doesn't immediately explain why a module defined within the doctest is incapable of accessing a function that is also defined within the doctest to me</p>



<a name="214121198"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Doctest%20weirdness/near/214121198" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Doctest.20weirdness.html#214121198">(Oct 21 2020 at 22:08)</a>:</h4>
<p>Add an <code>fn main () {}</code> item definition, otherwise you code gets wrapped in one, and then you are hitting the fact that relative paths can't refer to items defined inside a function</p>



<a name="214121313"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Doctest%20weirdness/near/214121313" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> CraftSpider <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Doctest.20weirdness.html#214121313">(Oct 21 2020 at 22:09)</a>:</h4>
<p>Oh, yep, that fixes it. Interesting, I didn't know that that was how doctest worked. Thank you for the help.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>